#include "EvalBleedThroughVersoQuantity.hpp"

#include <assert.h>


EvalBleedThroughVersoQuantity::EvalBleedThroughVersoQuantity(const girl::tools::Image &verso)
    :_verso(verso)
{}

/**
  * @todo Mettre au propre !!!!!!
  */
float EvalBleedThroughVersoQuantity::evaluate(const girl::tools::Image &image)
{
    nbPixelEncreVerso = 0;

    ise::GrayScaleHistogram h;
    h.reset(_verso,0,0);

    ise::QuantificationThresholderOtsu1T otsuLocal;
    std::vector<int> levels;
    otsuLocal.getLevels(h,levels);

    assert(_verso.width() > 0);

    for(int i = 0; i< _verso.width(); i++){
        for(int j = 0; j< _verso.height(); j++){
            int pixel = _verso.getPixel(i,j).a();
            int rectoPixel = image.getPixel(i,j).a();
            if(pixel < levels[0] && rectoPixel > 100)
                nbPixelEncreVerso++;
        }
    }
    std::cout << "At Verso" <<  nbPixelEncreVerso << std::endl;

    int nbPixelEncreRecto = 0;
    for(int i = 0; i< image.width(); i++){
        for(int j = 0; j< image.height(); j++){
            int pixel = image.getPixel(i,j).a();
            if(pixel < 100)
                nbPixelEncreRecto++;
        }
    }
    /*float inkVersoDensity = nbPixelEncreVerso /(_verso.width()*_verso.height());
    float inkRectoDensity = nbPixelEncreRecto /(image.width()*image.height());
    std::cout << "QuantityProba " << inkVersoDensity << "/" << inkRectoDensity << std::endl;

    if(inkRectoDensity == 0)
        return 1;
    if(inkVersoDensity ==0)
        return 0;
*/

    return nbPixelEncreVerso / nbPixelEncreRecto;

}
